package com.nrtech.brebes;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.readystatesoftware.mapviewballoons.BalloonItemizedOverlay;

public class SimpleItemizedOverlay extends BalloonItemizedOverlay<OverlayItem> {
	private ArrayList<OverlayItem> m_overlays = new ArrayList<OverlayItem>();
	private ArrayList<String> markerDescription = new ArrayList<String>();
	private ArrayList<String> markerId = new ArrayList<String>();
	private Context c;	
	private MainActivity mainAct;
	
	public SimpleItemizedOverlay(Drawable defaultMarker, MapView mapView, MainActivity theAct) {
		super(boundCenter(defaultMarker), mapView);
		c = mapView.getContext();		
		mainAct = theAct; 
	}
	
	public void addOverlay(OverlayItem overlay) {
	    m_overlays.add(overlay);
	    populate();
	}

	@Override
	protected OverlayItem createItem(int i) {
		// TODO Auto-generated method stub
		return m_overlays.get(i);
	}

	@Override
	public int size() {
		// TODO Auto-generated method stub
		return m_overlays.size();
	}
	
	// Removes overlay item i
    public void removeItem(int i){
    	m_overlays.remove(i);
    	markerDescription.remove(i);
    	markerId.remove(i);
        populate();
    }

	@Override
	protected boolean onBalloonTap(int index, OverlayItem item) {
		//Toast.makeText(c, markerDescription.get(index) + "",
		//		Toast.LENGTH_LONG).show();
		mainAct.callInfoView(markerId.get(index));
		return true;
	}
	
	public void addOverlayItem(double lat, double lng, String title, String Id, String desc) {
        try {
            GeoPoint point = new GeoPoint(
                    (int) (lat * 1E6), 
                    (int) (lng * 1E6));
            OverlayItem overlayItem = new OverlayItem(point, title, null);
            markerDescription.add(desc);
            markerId.add(Id);
            addOverlay(overlayItem);    
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }	
}
